<template>
  <div>
    <search-table
      :url="url"
      ref="searchtable"
      :columns="columns"
      :defaultParams="defaultParams"
      :headParams="headParams"
      :form="form">
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="活动名称">
            <a-input allowClear v-decorator="['activityName']" placeholder="请输入活动名称"></a-input>
          </a-form-item>
          <a-form-item label="状态">
            <a-select
              showSearch
              allowClear
              style="width: 150px"
              :options="statusList"
              v-decorator="['activityStatus']"
              placeholder="请选择">
            </a-select>
          </a-form-item>
          <!-- <a-form-item label="是否参与活动">
            <a-select
              allowClear
              style="width: 150px"
              :options="yesOrNo"
              v-decorator="['isIn']"
              placeholder="请选择">
            </a-select>
          </a-form-item> -->
        </a-form>
      </template>
      <template slot="oprationRow">
        <permission authority="marketing:activity:add">
          <a-button type="primary" @click="editInfo('add', {})">新增活动</a-button>
        </permission>
      </template>
      <template slot="tableOperation" slot-scope="{ record }">
        <a-space>
          <permission v-if="record.activityStatus != 1" authority="marketing:activity:info">
            <span
              class="text-primary pointer"
              @click="editInfo('view', record)"
              >查看</span>
            <!-- <a-button type="link">
              查看
            </a-button> -->
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission v-if="record.activityStatus == 1" authority="marketing:activity:modify">
            <span
              class="text-primary pointer"
              @click="editInfo('edit', record)"
              >编辑</span>
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission v-if="record.activityStatus == 2" authority="marketing:activity:modify">
            <span
              class="text-primary pointer"
              @click="handleConfirm('end', record)"
              >活动停止</span>
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission v-if="record.activityStatus != 2" authority="marketing:activity:delete">
            <a-popconfirm
              title="是否确认删除？"
              :getPopupContainer="getPopupContainer()"
              @confirm="() => deleteRows([record.id])">
              <a href="javascript:;" class="text-danger pointer">删除</a>
            </a-popconfirm>
          </permission>
        </a-space>
      </template>
    </search-table>
  </div>
</template>

<script>
import { findDictNameByValue,createOptionsByDict } from '@/utils/dict'
// import seckillForm from './components/form';
export default {
  name: 'MarketingSeckill',
  components: {},
  // props: {
  //   // 自身的tab
  //   tab: {
  //     type: String,
  //     required: true,
  //     default: ''
  //   },
  // },
  data() {
    return {
      statusList: createOptionsByDict('MARKETING_STATUS'),
      yesOrNo: createOptionsByDict('YES_NO'),
      url: '/marketing/activity/page',
      headParams: { baseURL: `${location.origin}/goldenhome/seckill/admin` },
      deleteUrl: "/marketing/activity/delete",
      handle: 'edit',
      currentRecord: {},
      form: this.$form.createForm(this, { name: "table_search" }),
      defaultParams: {},
      columns: [
        {
          title: "活动名称",
          dataIndex: "activityName",
          key: "activityName",
        },
        {
          title: "活动类型",
          dataIndex: "type",
          key: "type",
          customRender: (text)=>{
            let res = ''
            if(text==1){
              res = '第二件半价'
            } else if(text==2){
              res = '买一赠一'
            }
            return res;
          }
        },
        {
          title: "活动状态",
          dataIndex: "activityStatus",
          key: "activityStatus",
          customRender: (text, record) => findDictNameByValue('MARKETING_STATUS', record.activityStatus)
        },
        {
          title: "支付订单笔数",
          dataIndex: "orderPayNum",
          key: "orderPayNum"
        },
        {
          title: "操作",
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        }
      ]
    };
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getPopupContainer() {
      return (triggerNode) => triggerNode.parentNode.parentNode;
    },
    closeModal(freshTable) {
      if (freshTable) {
        this.$refs.searchtable.refreshTable()
      }
    },
    handleConfirm(type, record) {
      let _this = this
      
      this.$confirm({
        title: '活动停止',
        content: '停止进行中的活动会影响买家正常购买，建议您提前告知或说明，确定要停止活动吗？',
        okText: '确认停止',
        cancelText: '取消',
        onOk() {
          record.activityStatus = 3
          _this.$post('/marketing/activity/update',
            record,
            { baseURL: `${location.origin}/goldenhome/seckill/admin` }).then((res) => {
            if (res && res.returnCode == 1000) {
              _this.$refs.searchtable.refreshTable();
            }
          });
        },
        onCancel() {},
      });
    },
    editInfo(type, record) {
      this.currentRecord = record
      this.$router.push({
        name:'MarketingCampaignPagesForm',
        query: {
          handle: type,
          id: record.id
        }
      })
    },
    deleteRows(ids) {
      this.$post(this.deleteUrl,
        ids,
        { baseURL: `${location.origin}/goldenhome/seckill/admin` }).then((res) => {
        if (res && res.returnCode === 1000) {
          this.$refs.searchtable.refreshTable();
        }
      });
    },
  }
};
</script>

<style lang="less" scoped></style>
